package workderservice

import (
	"fmt"
	"goruntimeDistributionCrawler/engine"
	"goruntimeDistributionCrawler/zhenai/parser"
)

type Serialized struct {
	FunctionName string
	Args interface{}
}

type Request struct {
	Url string
	Parser Serialized
}

type ParseResult struct {
	Items []engine.Item
	Request []Request
}

func SerializeRequst(r engine.Request) Request  {
	name,args:=r.Parser.Serialized()
	return Request{
		Url:r.UrL,
		Parser:Serialized{
			FunctionName:name,
			Args:args,},
	}
}

func SerializeResult(
	r engine.ParseResult) ParseResult  {
	result:=ParseResult{
		Items:r.Items,
	}
	for _,req:=range r.Requests{
		result.Request=append(
			result.Request,SerializeRequst(req))
	}

	return result
}

func DeserializeRequst(r  Request) (engine.Request ,error) {
	parser,err:=deserialzeParser(r.Parser)
	if err!=nil {
		return engine.Request{},err
	}
	return engine.Request{
	  UrL:r.Url,
	  Parser:parser,
	},nil
}

func DeserializeResult(r ParseResult) engine.ParseResult  {
	result:=engine.ParseResult{
		Items:r.Items,
	}
	for _,req:=range r.Request{
		  req,err:=DeserializeRequst(req)
		  if err ==nil{
			  result.Requests=append(result.Requests,
				  req)
		  }
	}
	return result
}

func deserialzeParser(p Serialized) (engine.Parser,error)  {
	switch p.FunctionName {
		case "ParserProfilList":
			return engine.NewParserFunc(parser.ParserProfilList,"ParserProfilList"),nil
		case "ParsePorfile":
			return engine.NewParserFunc(parser.ParsePorfile,"ParsePorfile"),nil
		case "PaseCityList":
			return engine.NewParserFunc(parser.PaseCityList,"PaseCityList"),nil
		default:
			return engine.NilParser{},fmt.Errorf("No Manager Func")
	}
}


